package team.szm.scd.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Component
public class EnvUtils {

    public enum EnvType {
        DEV("dev"),
        TEST("test"),
        PROD("prod"),
        UNKNOWN("unknown");
        public final String value;

        EnvType(String value) {
            this.value = value;
        }
    }

    private static String envStr;

    private static boolean firstInvoke = true;
    private static EnvType envType = EnvType.UNKNOWN;

    public static void setEnv(Environment env) {
        envStr = env.getProperty("spring.profiles.active");
    }

    public static EnvType getEnv() {
        if (firstInvoke) {
            firstInvoke = false;
            envType = Arrays.stream(EnvType.values()).filter(envType -> envType.value.equals(envStr))
                    .findAny().orElse(EnvType.UNKNOWN);
        }
        return envType;
    }

    public static void AssertEnv(EnvType type) throws IllegalAccessException {
        if (type != getEnv()) {
            throw new IllegalAccessException();
        }
    }

}
